package com.yaorange.response;

import com.yaorange.response.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 15332
 * @version 1.0
 * @date 2025/6/16 17:01
 * @description 相应结果
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
  /**
   * 状态码
   */
  private int code;
  /**
   * 信息
   */
  private String message;
  /**
   * 数据
   */
  private T data;

  /**
   * 使用 ResultCode 构造成功/失败响应
   */

  public static <T> Result<T> of(ResultCode resultCode) {
    return new Result<>(resultCode.getCode(), resultCode.getMessage(), null);
  }

  public static <T> Result<T> of(ResultCode resultCode, T data) {
    return new Result<>(resultCode.getCode(), resultCode.getMessage(), data);
  }

  /**
   * 快捷方法：返回成功结果
   */
  public static <T> Result<T> success(T data) {
    return of(ResultCode.SUCCESS, data);
  }

  public static <T> Result<T> success() {
    return of(ResultCode.SUCCESS, null);
  }

  /**
   * 快捷方法：返回失败结果
   */
  public static <T> Result<T> fail() {
    return of(ResultCode.FAIL);
  }

  public static <T> Result<T> fail(ResultCode resultCode) {
    return of(resultCode);
  }

  public static <T> Result<T> fail(String message) {
    Result result = new Result();
    result.setCode(200);
    result.setMessage(message);
    return result;
  }

  public static <T> Result<T> fail(int code, String message){
    Result result = new Result();
    result.setCode(code);
    result.setMessage(message);
    return result;
  }


}
